/*******************************************************************************
 *
 * Copyright (c) 2008-2011 ayound@gmail.com, guwei8576431@gmail.com
 * This program and the accompanying materials
 * are made available under the terms of the GNU GENERAL PUBLIC LICENSE 
 * which accompanies this distribution, and is available at
 * http://dev.perl.org/licenses/gpl1.html
 * All rights reserved.
 * 
 * Created on 2009-10-22
 *******************************************************************************/

package org.cgtw.scriptprofile.server.servlet.processor;

import java.net.MalformedURLException;
import java.net.URL;

import org.cgtw.scriptprofile.server.manager.ScriptProfileServerManager;
import org.cgtw.scriptprofile.server.servlet.ScriptProfileRequest;
import org.cgtw.scriptprofile.server.servlet.ScriptProfileResponse;
import org.cgtw.scriptprofile.server.servlet.ScriptProfileResponseInfo;

public class ProcessorFactory {
	private static ProcessorFactory instance = null;
	private static String PROFILE_FLAG = "/cgtw.profile";
	private static String JAVASCRIPT_FLAG = "text/javascript";
	private static String HTML_FLAG = "text/html";
	
	private ProcessorFactory(){}
	
	public static ProcessorFactory getInstance(){
		if(instance == null){
			instance = new ProcessorFactory();
		}
		return instance;
	}
	
	/**
	 * create processor by different url "/" or baseUrl
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	public IServerProcessor createProcessor(ScriptProfileRequest request,ScriptProfileResponse response) {
		if (request.getRequestSource().startsWith(PROFILE_FLAG)) {
			return new ProfileProcessor(request, response);
		} else {
			URL url = getRemoteURL(request.getRequestSource());
			ScriptProfileResponseInfo info = ProcessorUtil.getResponseInfo(url, request);
			String contentType = info.getContentType();
			if (info.getResponseHeader() != null) {
				if (info.getResponseHeader().containsKey("Location")) {
					response.writeHTMLHeader(null, info.getResponseHeader());
					response.close();
					return null;
				}
			}
			if (contentType != null
					&& contentType.indexOf(JAVASCRIPT_FLAG) > -1) {
				return new ScriptProcessor(request, response, info);
			} else if (contentType != null
					&& contentType.indexOf(HTML_FLAG) > -1) {
				return new HtmlProcessor(request, response, info);
			} else {
				return new ResourceProcessor(request, response, info);
			}

		}
	}

	/**
	 * compute the remote url. the request url maybe
	 * http://localhost:8080/test/a.html the method convert it to
	 * http://www.site.com/test/a.html
	 * 
	 * @param resource
	 * @param server
	 * @return
	 */
	public static URL getRemoteURL(String resource) {
		String path = resource.replace(ScriptProfileServerManager.getInstance().getServer().getLocalBaseUrl(), "");
		try {
			return new URL(ScriptProfileServerManager.getInstance().getServer().getRemoteBaseUrl(), path);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
